<?php
/*
* Copyright (c) 2010 by Cediti
*
* You may distribute under the terms of either the GNU Lesser General
* Public License or the new BSD License, as specified in the README file.
*/
// created :	21 April 2010
// version :	21 April 2010

/**
 * Used to retrieve the allowable actions for the object(s) described in the service response.
 */
class Cmis_Repository_AllowableActions implements Countable
{
	/**
	 * @var array[Cmis_Repository_AllowableActions_AllowableAction]
	 */
    protected $_allowableActions;

	public function __construct()
	{
		$this->_init();
	}

	public function count()
	{
		return count($this->_allowableActions);
	}

	/**
	 * Extract allowableActions from xml stream
	 * @param SimpleXMLElement $response
	 * @return Cmis_Repository_AllowableActions
	 */
	public function setXmlResponse(SimpleXMLElement $response)
	{
		$allowables = $response->xpath('//cmis:allowableActions');

		//if no return or error just gfinish it....
		if (!empty($allowables))
		{
			$allowables = $allowables[0]->children('cmis', true);
			foreach($allowables as $key => $value)
			{
				$this->add($key, $value);
			}
		}

		return $this;
	}

	/**
	 * Add an allowableAction
	 * @param string $key
	 * @param bool $value
	 * @return Cmis_Repository_AllowableActions
	 */
	public function add($key, $value)
	{
		$this->_allowableActions[ $key ] = new Cmis_Repository_AllowableActions_AllowableAction($key, (bool)$value);
		return $this;
	}

	/**
	 * Get the allowableAction(s)'s value if null return all the set
	 * @param string $key
	 * @return bool|array[Cmis_Repository_AllowableAction]
	 */
	public function get($key = null)
	{
        if (!empty($key) && isset($this->_allowableActions[ $key ]))
        {
            return $this->_allowableActions[ $key ]->isAllowable();
        }
        return $this->_allowableActions;
	}

    public function __toString()
    {
    	$ret = '';
    	foreach($this->_allowableActions as $allowable)
    	{
    		$ret .= $allowable;
    	}
    	return $ret;
    }

	protected function _init()
	{
		$this->_allowableActions = array();
	}
}

# EOF
